﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelnetRouterApplication.Model;
using TelnetRouterApplication.Control;

namespace TelnetRouterApplication.View
{
    public partial class InterfaceStatus : UserControl
    {
        private MainForm _mainForm;
        public MainForm MainForm
        {
            get { return _mainForm; }
            set { _mainForm = value; }
        }

        private List<Interface> _interfaceList;
        public List<Interface> InterfaceList
        {
            get { return _interfaceList; }
            set { _interfaceList = value; }
        }

        public InterfaceStatus() : this(null) { }

        public InterfaceStatus(MainForm mainForm)
        {
            MainForm = mainForm;
            InitializeComponent();
            interfaceListBox.DrawMode = DrawMode.OwnerDrawFixed;
            interfaceListBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.interfaceListBoxDrawItem);
            RefreshList();
        }

        private void RefreshList()
        {
            InterfaceList = CommandHandler.GetInstance().GetInterfaces();
            interfaceListBox.Items.Clear();
            if (InterfaceList.Count > 0)
            {
                interfaceListBox.BeginUpdate();
                foreach (Interface inter in InterfaceList)
                {
                    interfaceListBox.Items.Add(inter);
                }
                interfaceListBox.EndUpdate();
            }
        }

        private void refreshBtn_Click(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void testBtn_Click(object sender, EventArgs e)
        {
            MainForm.CallTestInterfacesForm();
        }

        private void interfaceListBoxDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            // Draw the background of the ListBox control for each item.
            // Create a new Brush and initialize to a Black colored brush
            // by default.

            e.DrawBackground();
            Brush myBrush = Brushes.Black;

            // Determine the color of the brush to draw each item based on 
            // the index of the item to draw.

            Interface inter = (Interface) interfaceListBox.Items[e.Index];

            if (inter.IsTurnedON())
            {
                myBrush = Brushes.Green;
            }
            else
            {
                myBrush = Brushes.Red;
            }

            // Draw the current item text based on the current 
            // Font and the custom brush settings.
            //

            e.Graphics.DrawString("Interface: " + inter.Name + " IP: " + inter.Ip + " Status: " + inter.IsTurnedON(),
            e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);

            // If the ListBox has focus, draw a focus rectangle 
            // around the selected item.

            e.DrawFocusRectangle();
        }
    }
}
